package three;

import org.junit.jupiter.api.Test;

public class TwoHundred {

    // 202. 快乐数
    @Test
    public void test() {
        System.out.println(this.isHappy(19));
        System.out.println(this.isHappy(2));
    }

    public boolean isHappy(int n) {
        int turtle = n, rabbit = nextInt(n);
        ;
        while (rabbit != 1 && turtle != rabbit) {
            turtle = nextInt(turtle);
            rabbit = nextInt(nextInt(rabbit));
        }
        return rabbit == 1;
    }

    public int nextInt(int n) {
        int nextInt = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            nextInt += d * d;
        }
        return nextInt;
    }

    // 206. 反转链表
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 永远返回的是最后一个节点，也就是新节点的头
        ListNode listNode = reverseList(head.next);
        // 当前节点的孩子孩子指向自己
        head.next.next = head;
        // 赋一下值不然是循环链表
        head.next = null;
        return listNode;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
